package com.jn.financing;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jn.common.base.BaseFragment;
import com.jn.common.database.DBHelper;
import com.jn.common.utils.SpUtils;

public class OrdersFragment extends BaseFragment implements View.OnClickListener {
    private TextView mCountView;
    private EditText mInput;
    private Button mBuyButton;
    private Button mSellButton;
    private int count;
    private SpUtils mSpUtils;
    private DBHelper mDbHelper;
    private String userName;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_orders;
    }

    @Override
    protected void init() {
        mCountView = mView.findViewById(R.id.count);
        mInput = mView.findViewById(R.id.input);
        mBuyButton = mView.findViewById(R.id.buy_button);
        mSellButton = mView.findViewById(R.id.sell_button);
        mBuyButton.setOnClickListener(this);
        mSellButton.setOnClickListener(this);
        //mSpUtils = new SpUtils(getContext());
        //count = mSpUtils.getInt("count", 0);
        mDbHelper = DBHelper.getInstance(getContext());
        userName = mDbHelper.getLoginUser();
        count = mDbHelper.getMoneyByName(userName);
        mCountView.setText(String.valueOf(count));
    }

    @Override
    protected void onFragmentChanged() {
        userName = mDbHelper.getLoginUser();
        count = mDbHelper.getMoneyByName(userName);
        mCountView.setText(String.valueOf(count));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        int inputCount = Integer.parseInt(mInput.getText().toString());
        if (id == R.id.buy_button) {
            count += inputCount;
        } else if (id == R.id.sell_button) {
            count -= inputCount;
        }
        mCountView.setText(String.valueOf(count));
        //mSpUtils.putInt("count", count);
        mDbHelper.updateMoneyByName(userName, count);
    }
}
